import PlayerDataSys from "./PlayerDataSys";
import EventMgr from "./EventMgr";
import GameEventType from "./GameEventType";
import EngineUtil from "./EngineUtil";
import BasePageCtrl, { AnimType } from "./BasePageCtrl";
import PeripheralSystemMgr from "./PeripheralSystemMgr";
import UiManage from "./UiManage";
import ToastPage from "./ToastPage";
const {ccclass, property} = cc._decorator;

@ccclass
export default class ToastPageCtrl extends BasePageCtrl {

    public onLoad() {
        this.onUILoad();
        this._animTime = 1;
        this._animType = AnimType.NONE;
        this._touchControl = false;
        this._hasPeneLock = true;
        this._hasBlack = false;
        this._hasTouchLock = false;
        this._hasBlackTouch = false;
        super.onLoad();
        this.addButtonListen();
    }

    public start() {}

    public onUILoad() {
        this.ui = this.node.addComponent(ToastPage);
    }

    public addButtonListen() {}

    public _init(e?) {
        debugger
        if (e) {
            const a = e.type;
            const o = e.count;
            const i = e.is_travel;
            const r = e.page_type;
            if (i) {
                if (a === 1) {
                    this.ui.countLabel.getComponent(cc.Label).string = "+" + EngineUtil.getCashBalance(o) + "";
                } else if (a === 2) {
                    this.ui.countLabel.getComponent(cc.Label).string = "+ " + EngineUtil.getCashBalance(o) + " ";
                }
            } else {
                this.ui.countLabel.getComponent(cc.Label).string = "+" + EngineUtil.getCashBalance(o) + "";
            }

            const u = "reward_" + a;
            UiManage.loadSpriteFrame(this.ui.reward, "toast", u);

            this.scheduleOnce(() => {
                if (i) {
                    if (a === 1) {
                        EventMgr.trigger(GameEventType.FLY_REWARD_CASH, { pos: this.ui.reward.getPosition() });
                    } else if (a === 2) {
                        EventMgr.trigger(GameEventType.FLY_REWARD_FUND, { pos: this.ui.reward.getPosition() });
                    }
                } else if (r !== 1 && r !== 2) {
                    EventMgr.trigger(GameEventType.SHOWEFFECT_FLYINGRED, {
                        startPos: cc.v2(0, 0),
                        type: 0,
                        callback: () => {
                            PlayerDataSys.updateCashBalace(o);
                            if (r === 2) {
                                PeripheralSystemMgr.getClockTaskList(true);
                            }
                        }
                    });
                }
            }, 0.8);

            this.scheduleOnce(() => {
                this.hide();
            }, 1.1);
        }
    }

    public clickClose() {
        this.hide();
        this.ui.reward.getComponent(cc.Sprite).spriteFrame = null;
    }
}
